<?php
class TestController extends \Yaf\Controller_Abstract {
	/**
	 * @var \TestModel
	 */
	public $_model;
	public function init() {
		$this->_model = new \TestModel();
	}
	/**
	 * 配置文件读取
	 * @return bool
	 */
	public function configAction(){
		$config = \Yaf\Application::app()->getConfig()->toArray();
		echo json_encode($config['db'], JSON_UNESCAPED_UNICODE);
		return false;
	}

	/**
	 * list
	 */
	public function listAction() {
		if($this->getRequest()->isPost()){
			$title=$this->getRequest()->getPost("title", '');
			$content=$this->getRequest()->getPost("content", '');
			$id=$this->_model->create(['title'=>$title, 'content'=>$content]);
			echo json_encode(['id'=>$id], JSON_UNESCAPED_UNICODE);
		}else{
			$page=$this->getRequest()->getQuery("page", 1);
			$max=$this->getRequest()->getQuery("max", 10);
			$result=$this->_model->list($page, $max);
			echo json_encode(['count'=>$result[0], 'list'=>$result[1]], JSON_UNESCAPED_UNICODE);
		}
		return false;
	}
	/**
	 * 添加
	 */
	public function saveAction(){
		//暂时无解，先和list用一个action
		return false;
	}
	/**
	 * 详情
	 * @param $id
	 */
	public function readAction($id){
		if($this->getRequest()->isDelete()){
			//delete
			$this->_model->delete($id);
			echo json_encode(['delete'=>true], JSON_UNESCAPED_UNICODE);
		}elseif($this->getRequest()->isPut()){
			//update
			$title='a';//put请求，参数接收暂时无解
			$content='b';
			$this->_model->update($id, ['title'=>$title, 'content'=>$content]);
			echo json_encode(['update'=>true], JSON_UNESCAPED_UNICODE);
		}else{
			//detail
			$result=$this->_model->get($id);
			echo json_encode($result, JSON_UNESCAPED_UNICODE);
		}
		return false;
	}
	/**
	 * 更新
	 * @param $id
	 */
	public function updateAction($id){
		//暂时无解，先和read用一个action
		return false;
	}
	/**
	 * 删除
	 */
	public function deleteAction($id){
		//暂时无解，先和read用一个action
		return false;
	}
}

